package base.stream;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Stream api 测试
 *
 * @author scylla
 * @version V1.0
 * @project echo-java-base
 * @title Stream测试类
 * @package base.stream
 * @since 2022/6/7 4:15 下午
 */
public class StreamDemo {


    public static void main(String[] args) {
        //现在需要取出 leaveList 中每个对象的第四个集合属性。并合并为一个List
        Leave leave1 = new Leave("1", "1", new Date(), Arrays.asList("A", "B"));
        Leave leave2 = new Leave("2", "2", new Date(), Arrays.asList("C", "D"));
        Leave leave3 = new Leave("3", "3", new Date(), Arrays.asList("E", "F"));

        List<Leave> leaveList = Arrays.asList(leave1, leave2, leave3);

        //仅使用map，会产生嵌套集合
        List<List<String>> mapResult = leaveList.stream().map(Leave::getStrings).collect(Collectors.toList());

        //flatMap 直接生成一个集合. 将相同的元素合并到一起
        List<String> result = leaveList.stream().flatMap(leave -> leave.getStrings().stream()).collect(Collectors.toList());

        //flatMap 将一起的元素拆分
        List<String> list = Arrays.asList("ABC", "DEF", "GHI");
        List<String> splitResult = list.stream().flatMap(ele -> Stream.of(ele.split(""))).collect(Collectors.toList());


        System.out.println(mapResult.toString() + result.toString() + splitResult.toString());
    }
}
